/*
 *  Copyright (C) 2010  Noxious Productions
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "game.hpp"

bool Game::Init()
{
#ifdef DEBUG
    cout << "Swordsmen - Copyright (C) 2010  Noxious Productions" << endl;
#endif

    // create window
    window = NULL;
    if (!ChangeVideoMode(800, 600, false))  // width and height need to be read from an option file so the user doesn't need to set them every time
        return false;   // error handling here

    if (!window)
        return false;   // error handling here

    // load the map file
    if (!mapTest.LoadMap("../../Data/Misc/mapTest.xml"))
        return false;   // error handling here

    isRunning = true;
    return true;
}

void Game::Execute()
{
    while (isRunning)
    {
        Loop();
        
        sf::Event Event;
        while (window->GetEvent(Event))
            EventHandler(Event);

        Render();
        //cout << 1/window->GetFrameTime() << endl;
    }

    CleanUp();
}

void Game::CleanUp()
{
    if (window != NULL)
    {
        window->Close();
        delete window;
    }
}

bool Game::ChangeVideoMode(unsigned int width, unsigned int height, bool fullscreen)
{
    sf::VideoMode mode(width, height);
     
    if (!mode.IsValid() && fullscreen)
        return false;

    if (window != NULL)
    {
        window->Close();
        delete window;
    }
#ifdef DEBUG
    window = new sf::RenderWindow(mode, "Swordmen - Copyright (C) 2010  Noxious Productions - Debug", fullscreen ? sf::Style::Fullscreen : sf::Style::Close);
#else
    window = new sf::RenderWindow(mode, "Swordmen - Copyright (C) 2010  Noxious Productions - Release", fullscreen ? sf::Style::Fullscreen : sf::Style::Close);
#endif

    return true;
}
